// 项目初始化数据-项目运行只走一次
import { defineStore } from "pinia";
import { IConfig,getConfig } from "@/api/env";

interface IState {
    config:IConfig | null,
    iframeBackBtnShow:boolean,
    depart:string,
    area:string,
    rq:string,
    indexPage:string | null
}

export const useConfigStore = defineStore({
    // 每个 store 的 id 必须唯一
    id: 'config',
    // state 表示数据源
    state: (): IState => ({
        config:null,
        iframeBackBtnShow:true,
        depart:"选择科室",
        area:"选择诊区",
        rq:"选择日期",
        indexPage:null,
    }),
    // actions 用来修改 state
    actions: {
        setIndexPage(indexPage:string){
          this.$patch((state)=>{
            state.indexPage = indexPage;
          })
        },
        setDepart(depart:string){
          this.$patch((state)=>{
            state.depart = depart;
          })
        },
        setArea(area:string){
          this.$patch((state)=>{
            state.area = area;
          })
        },
        setRq(rq:string){
          this.$patch((state)=>{
            state.rq = rq;
          })
        },
        async setIframeBackBtnShow(isShow=true){
          this.$patch((state)=>{
            state.iframeBackBtnShow = isShow;
          })
        },
        async getConfig(param?:object) {
          if(!(window as any).getConfig){
            return null;
          }
          const res =  await ((window as any).getConfig)(param);
          this.$patch((state)=>{
            state.config = res.data!
          })
        }
    },
})
